import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wanAndroid/bean/my_collection_bean.dart';
import 'package:wanAndroid/common/http/base_model.dart';
import 'package:wanAndroid/common/http/http_service.dart';
import 'package:wanAndroid/common/http/http_uts.dart';
import 'package:wanAndroid/common/uts/toast_uts.dart';
import 'package:wanAndroid/common/uts/ui_uts.dart';
import 'package:wanAndroid/page/page_webview.dart';
import 'package:wanAndroid/res/colors.dart';

class PageMyCollection extends StatefulWidget {
  //启动
  static Future<bool> start(BuildContext context,
      {Map<String, dynamic> args}) async {
    return await Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => PageMyCollection(),
        ));
  }

  @override
  State<StatefulWidget> createState() => _PageMyCollection();
}

class _PageMyCollection extends State<PageMyCollection> {
  int _pageCount = 0;
  int _page = 0;
  List<Datas> dts;
  RefreshController _refreshController;

  ///
  bool isUp = true;

  @override
  void initState() {
    _refreshController = RefreshController(initialRefresh: true);
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        backgroundColor: ResColors.scaffoldColor,
        appBar: AppBar(
          title: UiUts.getTitle("我的收藏"),
        ),
        body: Center(
          child: SmartRefresher(
            enablePullDown: true,
            enablePullUp: isUp,
            header: ClassicHeader(),
            footer: ClassicFooter(),
            controller: _refreshController,
            onLoading: () async {
              setState(() {
                _page += 1;
              });
              getCollectionData();
            },
            onRefresh: () async {
              setState(() {
                _page = 0;
              });
              getCollectionData();
            },
            child: null == dts
                ? UiUts.getEmpty()
                : ListView.separated(
                    itemBuilder: (context, index) {
                      final Datas d = dts[index];
                      return getItemView(d);
                    },
                    separatorBuilder: (context, index) {
                      return UiUts.getDivider(height: 5);
                    },
                    itemCount: dts.length),
          ),
        ),
      );

  Future<void> getCollectionData() async {
    HttpService.myCollectionData(_page,
        callback: HttpCallback((rsp) {
          UiUts.finishRefresh(_refreshController);
          MyCollectionBean dt = MyCollectionBean.fromJson(rsp.data);
          if (HttpUts.isSuccess(dt.errorCode)) {
            setState(() {
              _pageCount = dt.data.pageCount;
              if (0 == _page) {
                this.dts = dt.data.datas;
              } else {
                this.dts.addAll(dt.data.datas);
              }
              isUp = !((_page + 1) == _pageCount);
            });
          } else {
            ToastUts.showToast(context, text: "${dt.errorMsg}");
          }
        }, (err) {
          UiUts.finishRefresh(_refreshController);
          ToastUts.showToast(context, text: "$err");
        }));
  }

  /// item
  Widget getItemView(Datas dt) {
    return InkWell(
      onTap: () {
        PageWebView.start(
          context,
          dt.link,
          title: "${dt.title}",
        );
      },
      child: Container(
        width: double.infinity,
        padding: const EdgeInsets.only(
          left: 15,
          right: 10,
          top: 10,
          bottom: 0,
        ),
        constraints: BoxConstraints(minHeight: 50),
        alignment: Alignment.centerLeft,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(0),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            UiUts.getText(
              "${dt.title}",
              color: ResColors.fontColor,
              fontSize: 17,
            ),
            UiUts.getDivider(
              height: 5,
            ),
            UiUts.getText(
              "${dt.author} - ${dt.chapterName}",
              color: ResColors.fontColor,
              fontSize: 15,
            ),
            UiUts.getDivider(
              height: 5,
            ),
            UiUts.getText(
              "${dt.niceDate}",
              color: ResColors.fontColor,
              fontSize: 15,
            ),
            Container(
              alignment: Alignment.centerRight,
              width: double.infinity,
              constraints: BoxConstraints(
                minHeight: 30,
                minWidth: 100,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Container(
                    padding: const EdgeInsets.only(
                      right: 10,
                    ),
                    alignment: Alignment.center,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          Icons.thumb_up,
                          color: dt.zan > 0 ? Colors.redAccent : Colors.grey,
                          size: 18,
                        ),
                        Padding(
                          padding: const EdgeInsets.only(
                            right: 15,
                            left: 5,
                          ),
                          child: UiUts.getText(
                            "${dt.zan}",
                            fontSize: 12,
                            color: dt.zan > 0 ? Colors.redAccent : Colors.grey,
                          ),
                        ),
                      ],
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      HttpService.unCollect("${dt.id}",
                          callback: HttpCallback((rsp) {
                            BaseModel base = BaseModel.fromJson(rsp.data);
                            if (HttpUts.isSuccess(base.errorCode)) {
                              _page = 0;
                              ToastUts.showToast(context, text: "取消成功");
                              getCollectionData();
                            }
                          }, (err) {
                            ToastUts.showToast(context, text: "$err");
                          }));
                    },
                    child: Row(
                      children: [
                        Icon(
                          Icons.favorite,
                          color: Colors.redAccent,
                          size: 18,
                        ),
                        Padding(
                          padding: const EdgeInsets.only(
                            right: 10,
                            left: 5,
                          ),
                          child: UiUts.getText(
                            "收藏",
                            fontSize: 12,
                            color: ResColors.fontColor,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
